/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      XMLParser.hpp
 *
 * $FTS$
 */
#ifndef _XML_PARSER_HPP__
#define _XML_PARSER_HPP__ 1

// C Includes
#include <expat.h>
#include <iconv.h>

// Local Includes
#include <XMLHandler.hpp>

namespace FTSS // FullTextSearchSystem
{
#define C_XML_PARSER_BUFFER 8192

#ifndef XMLCALL
#define XMLCALL
#endif

/**
  @class XMLParser XMLParser.hpp <XMLParser.hpp>
  @brief XML Parser. Based on libExpat library.
*/
class XMLParser
{
public:
	/**
	  @brief Constructor
	  @param pIHandler - pointer to user handler object
	*/
	XMLParser(XMLHandler * pIHandler);

	/**
	  @brief Initialise stream parser
	*/
	void InitStream();

	/**
	  @brief Parse a block of the stream
	  @param sData - data buffer
	  @param iDataLength - buffer size
	*/
	INT_32 ParseStream(CCHAR_P sData, const UINT_32 & iDataLength);

	/**
	  @brief Finalise stream parser
	*/
	INT_32 CloseStream();

	/**
	  @brief Parse document
	  @param sData - data buffer
	  @param iDataLength - buffer size
	*/
	INT_32 ParseDocument(CCHAR_P sData, const UINT_32 & iDataLength);

	/**
	  @brief Parse document
	  @param F - file stream
	*/
	INT_32 ParseFile(FILE * F);

	/** @brief A destructor */
	~XMLParser();
private:
	/** F file stream flag         */
	bool           bInStream;
	/** Tag level                  */
	INT_32         iLevel;
	/** Abort flag                 */
	bool           bAbort;
	/** libExpat XML Parser object */
	XML_Parser     oParser;
	/** User defined XML handler   */
	XMLHandler   * pHandler;

	/** Forward map for KOI8-R encoding       */
	static UINT_16 sFWDKOI8_RMap[256];

	/** Forward map for CP866 encoding        */
	static UINT_16 sFWDCP866Map[256];

	/** Forward map for Windows-1251 encoding */
	static UINT_16 sFWDWindows_1251Map[256];

	/** Forward map for CP1251 encoding       */
	static UINT_16 sFWDCP1251Map[256];

	/** Forward map for ISO-8859-5 encoding   */
	static UINT_16 sFWDISO_8859_5Map[256];

	/** Reverse map for 2-byte encodings      */
	static CHAR_8 sRevMap[65536];

	/** Encoding converter                    */
	iconv_t       oIconvConverter;
	/** Size of buffer for iconv converter    */
	INT_32        iMaxBufferSize;
	/** Buffer for iconv converter            */
	CHAR_P        sIconvBuffer;
	/**
	  @brief Set handler for unknown encoding
	  @param vThis - pointer to XML Parser object
	  @param szEncoding - encoding name
	  @param pEncoding - encoding data
	*/
	static int XMLCALL UnknownEncodingHandler(void * vThis, const XML_Char * szEncoding, XML_Encoding * pEncoding);

	/**
	  @brief Start of element
	  @param vThis - pointer to XML Parser object
	  @param szName - XML tag name
	  @param szAttr - XML tag attributes
	*/
	static void XMLCALL StartElement(void * vThis, const char * szName, const char ** szAttr);
	/**
	  @brief End of Element
	  @param vThis - pointer to XML Parser object
	  @param szName - XML tag name
	*/
	static void XMLCALL EndElement(void * vThis, const char * szName);
	/**
	  @brief Charactes inside element
	  @param vThis - pointer to XML Parser object
	  @param szData - Characters between tags
	  @param iLength - Characters count
	*/
	static void XMLCALL Characters(void * vThis, const char * szData, INT_32 iLength);
};

} // namespace FTSS
#endif // _XML_PARSER_HPP__
// End.
